package cn.e3mall.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.utils.DateUtils;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.service.impl.ItemServiceImpl;

/**
 * 商品管理Controller
 * <p>Title: ItemController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class ItemController {

	@Autowired
	private ItemServiceImpl itemService;
	
	@RequestMapping("/item/{itemId}")
	@ResponseBody
	public TbItem getItemById(@PathVariable Long itemId) {
		TbItem tbItem = itemService.getItemById(itemId);
		return tbItem;
	}
	
	@RequestMapping("/item/list")
	@ResponseBody
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//调用服务查询商品列表
		EasyUIDataGridResult result = itemService.getItemList(page, rows);
		return result;
	}
	
	@RequestMapping("/item/search")
	@ResponseBody
	public EasyUIDataGridResult getItemsearch( String title,String created, String updated) {
		//调用服务查询商品列表
	    Date create = DateUtils.formatStringToDate(created, "yyyy-MM-dd");
	    Date update = DateUtils.formatStringToDate(updated, "yyyy-MM-dd");
		EasyUIDataGridResult result = itemService.getItemSearch(title, create, update, 1, 30);
		return result;
	}
	
	@RequestMapping("/item/param/list")
	@ResponseBody
	public EasyUIDataGridResult getItemParamList(Integer page, Integer rows) {
		//调用服务查询商品列表
		EasyUIDataGridResult result = itemService.getItemParamList(page, rows);
		return result;
	}
	
	/**
	 * 商品添加
	 */
	@RequestMapping(value="/item/save", method=RequestMethod.POST)
	@ResponseBody
	public E3Result addItem(TbItem item, String desc) {
		E3Result result = itemService.addItem(item, desc);
		return result;
	}
	
	/**
	 * 商品修改
	 */
	@RequestMapping(value="rest/item/update", method=RequestMethod.POST)
	@ResponseBody
	public E3Result updateItem(TbItem item, String desc) {
		E3Result result = itemService.updateItem(item, desc);
		return result;
	}
	
	/**
	 * 商品删除功能
	 */
	@RequestMapping(value="/rest/item/delete", method=RequestMethod.POST)
	@ResponseBody
	public E3Result deleteItem(long[] ids) {
		E3Result result = itemService.deleteItem(ids);
		return result;
	}
	
	/**
	 * 商品下架功能
	 */
	@RequestMapping(value="/rest/item/instock", method=RequestMethod.POST)
	@ResponseBody
	public E3Result itemInstock(long[] ids) {
		E3Result result = itemService.updateItemState(ids,2);
		return result;
	}
	
	/**
	 * 商品上架功能
	 */
	@RequestMapping(value="/rest/item/reshelf", method=RequestMethod.POST)
	@ResponseBody
	public E3Result itemReshelf(long[] ids) {
		E3Result result = itemService.updateItemState(ids,1);
		return result;
	}
	
	/**
	 * 商品修改跳转功能
	 */
	@RequestMapping(value="/rest/page/item-edit")
	public String itemEdit() {
		return "item-edit";
	}
	
	/**
	 * 商品详情
	 */
	@RequestMapping(value="/rest/item/query/item/desc/{itemId}")
	@ResponseBody
	public E3Result itemDesc(@PathVariable Long itemId) {
		E3Result result = itemService.itemDescE3Result(itemId);
		return result;
	}
	
	/**
	 * 商品规格
	 */
	@RequestMapping(value="/rest/item/param/item/query/{itemId}")
	@ResponseBody
	public E3Result itemParamQuery(@PathVariable Long itemId) {
		E3Result result = itemService.itemParamE3Result(itemId);
		return result;
	}
	
	/**
	 * 商品规格
	 */
	@RequestMapping(value="/item/param/query/itemcatid/{itemId}")
	@ResponseBody
	public E3Result itemCatQuery(@PathVariable Long itemId) {
		E3Result result = itemService.itemCatE3Result(itemId);
		return result;
	}
	
	
}
